package com.hunt.controller;

import com.hunt.model.dto.FundBaseMessageDto;
import com.hunt.model.dto.PageInfo;
import com.hunt.model.entity.FundBaseMessage;
import com.hunt.model.entity.FundBaseMessageAdd;
import com.hunt.service.FundBaseMessageAddService;
import com.hunt.service.FundBaseMessageService;
import com.hunt.service.MainDataService;
import com.hunt.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: sjs
 * @Date : 2020/8/31 17:21
 */
@Api(value = "手机app端,机构信息管理模块")
@Controller
@RequestMapping(path = "/app/fundMessage")
public class FundBaseMessageAddController extends BaseController {

    @Autowired
    @Resource
    private FundBaseMessageAddService fundBaseMessageAddService;

    @Autowired
    @Resource
    private FundBaseMessageService fundBaseMessageService;

    @Autowired
    @Resource
    private MainDataService mainDataService;

    @ApiOperation(value = "机构持股比例", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "findFundBaseMessageAdd", method = RequestMethod.GET)
    public Result findFundBaseMessageAdd(@RequestParam("stockCode") String stockCode) {

        List<FundBaseMessageAdd> fundBaseMessageAddList = fundBaseMessageAddService.findFundBaseMessageAdd(stockCode);

        if(fundBaseMessageAddList.size() > 0 ){
            FundBaseMessageAdd  mDta = fundBaseMessageAddList.get(0);
            if(stockCode.equals(mDta.getStockCode())) {

                if(mDta.getSocialHold()==null || mDta.getSocialHold().equals("")){
                    mDta.setSocialHold("0");
                }
                if(mDta.getTrustHold()==null || mDta.getTrustHold().equals("")){
                    mDta.setTrustHold("0");
                }
                if(mDta.getQfiiHold()==null || mDta.getQfiiHold().equals("")){
                    mDta.setQfiiHold("0");
                }
                if(mDta.getBrokerHold()==null || mDta.getBrokerHold().equals("")){
                    mDta.setBrokerHold("0");
                }
                if(mDta.getInsuranceHold()==null || mDta.getInsuranceHold().equals("")){
                    mDta.setInsuranceHold("0");
                }
                fundBaseMessageAddList.clear();
                fundBaseMessageAddList.add(mDta);
                return Result.success(fundBaseMessageAddList);
            }
        } else {
            FundBaseMessageAdd fundBaseMessageAdd = new FundBaseMessageAdd();

            ArrayList<FundBaseMessageAdd> list = new ArrayList<>();

            list.add(fundBaseMessageAdd);

            return Result.success(list);
        }

        return Result.success(fundBaseMessageAddList);
    }

    @ApiOperation(value = "查找基金持长比例", httpMethod = "GET", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getFundBaseMessageByStockCode", method = RequestMethod.GET)
    public Result getByStockCode(@RequestParam("stockCode") String stockCode) {

        FundBaseMessageDto fundBaseMessageDto = new FundBaseMessageDto();

        fundBaseMessageDto.setStockCode(stockCode);

        List<FundBaseMessage> fundBaseMessageList = fundBaseMessageService.findByStockCode(stockCode);

        if (null == fundBaseMessageList || fundBaseMessageList.size() ==0 ) {
            ArrayList<FundBaseMessage> fundBaseList = new ArrayList<>();
            FundBaseMessage fundBaseMessage = new FundBaseMessage();
            fundBaseList.add(fundBaseMessage);

            return  Result.success(fundBaseList);
        }

        return Result.success(fundBaseMessageList);
    }

    @ApiOperation(value = "查询主力数据列表", httpMethod = "GET", produces = "application/json", response = PageInfo.class)
    @ResponseBody
    @RequestMapping(value = "getMainDatas", method = RequestMethod.GET)
    public PageInfo list(@RequestParam(defaultValue = "1")@ApiParam(value = "页数数字") int page,
                         @RequestParam(defaultValue = "30")@ApiParam(value = "行数数字") int rows,
                         @RequestParam(defaultValue = "0")@ApiParam(value = "数字0到4,依次为ownFundNumber,holdingNumber,holdingStockValue,holdingChangeValue,holdingChangeRate") int fieldNo,
                         @RequestParam(defaultValue = "0") @ApiParam(value = "0为正向,1为反向")int sortNo) {
        PageInfo pageInfo = mainDataService.selectPage(page, rows, fieldNo, sortNo);
        return pageInfo;
    }

}